<template>
    <div>
        <a-modal title="添加版本" centered v-model:visible="visible" :confirm-loading="loading" :width="600" okText="保存" @ok="handleSave" @cancel="handleCancel">
            <a-form layout="inline" :model="form">
                <a-row>
                    <a-col :span="20">
                        <a-form-item label="JIRA Version" :span="24" name="systemName" v-if="visible">
                            <div>
                                <HSearchMultiSelectCell allowClear type="jiraVersion" label="name" keyField="id" :searchQuery="{ systemId: detail.systemId, released: type > 1 ? false : true, excludeJiraIds }" @success="handleSelectChange">
                                    <template #notFoundContent>
                                        <a-empty :image="simpleImage" description="未找到符合该状态的版本" />
                                    </template>
                                </HSearchMultiSelectCell>
                            </div>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </a-modal>
    </div>
</template>

<script lang="ts">
import api from '@/api';
import { message } from 'ant-design-vue/es';
import { HSearchMultiSelectCell } from '@/components';
import { Empty } from 'ant-design-vue';
import { defineComponent, reactive, toRefs } from 'vue';
export default defineComponent({
    components: {
        HSearchMultiSelectCell
    },
    emits: ['success'],
    setup(props, { emit }) {
        const state = reactive<any>({
            form: {
                systemName: []
            },
            list: [],
            visible: false,
            detail: {},
            type: 1,
            excludeJiraIds: '',
            loading: false,
            simpleImage: null
        });
        state.simpleImage = Empty.PRESENTED_IMAGE_SIMPLE;
        const open = (detail, type) => {
            handleReset();
            state.type = type;
            state.detail = {
                ...detail
            };
            let idArr = [];
            if (state.detail.releaseVersion && state.detail.releaseVersion.length) {
                const arr = state.detail.releaseVersion.map((item) => item.jiraId);
                idArr = idArr.concat(arr);
            }
            if (state.detail.processVersion && state.detail.processVersion.length) {
                const arr = state.detail.processVersion.map((item) => item.jiraId);
                idArr = idArr.concat(arr);
            }
            if (state.detail.processOverDueVersion && state.detail.processOverDueVersion.length) {
                const arr = state.detail.processOverDueVersion.map((item) => item.jiraId);
                idArr = idArr.concat(arr);
            }
            if (idArr.length) {
                state.excludeJiraIds = idArr.join(',');
            } else {
                state.excludeJiraIds = '';
            }
            state.visible = true;
        };
        const handleSelectChange = (val) => {
            state.list = [...val];
        };
        const handleSave = async () => {
            if (!state.list.length) {
                message.error('请勿提交空数据');
                return;
            }
            state.loading = true;
            const jiraIds: any = state.list.map((item) => item.id).join(',');
            const query: any = {
                jiraIds,
                type: 3,
                status: null,
                systemId: state.detail.systemId,
                systemReportId: state.detail.id
            };
            if (state.type === 1) {
                query.status = '已完成';
            } else if (state.type === 2) {
                query.status = '进行中';
            } else {
                query.status = '进行中延期';
            }
            const { success, errorMsg } = await api.systemReportModel.saveInfoFromJira(query);
            if (success) {
                state.visible = false;
                message.success('添加成功');
                emit('success');
            } else {
                message.error(errorMsg);
            }
            state.loading = false;
        };
        const handleCancel = () => {
            state.visible = false;
        };
        const handleReset = () => {
            state.form = {
                systemName: []
            };
            state.list = [];
        };
        return {
            ...toRefs(state),
            open,
            handleSelectChange,
            handleSave,
            handleCancel
        };
    }
});
</script>

<style scoped>
</style>